package cn.edu.zut.hkxy.intercept;

import cn.edu.zut.hkxy.pojo.Admin;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class PowerInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {
        System.out.println("->>>>>>>>>>>>>>>2.权限验证");
        HttpSession session=request.getSession();
        Admin admin= (Admin) session.getAttribute("admin");
        String power=admin.getPower();
        System.out.println("权限："+power);
        String uri=request.getRequestURI();
        System.out.println("请求链接："+uri);

        if(uri.contains("news")&&power.contains("1")){
            System.out.println("2.有权限>>>>>>>新闻");
            return true;
        }
        if(uri.contains("notice")&&power.contains("2")){
            System.out.println("2.有权限>>>>>>>通知");
            return true;
        }
        if(uri.contains("eduinfo")&&power.contains("3")){
            System.out.println("2.有权限>>>>>>>教务信息");
            return true;
        }
        if(uri.contains("stuinfo")&&power.contains("4")){
            System.out.println("2.有权限>>>>>>>学生工作");
            return true;
        }
        if(uri.contains("admissions")&&power.contains("5")){
            System.out.println("2.有权限>>>>>>>招生信息");
            return true;
        }
        if(uri.contains("jobs")&&power.contains("6")){
            System.out.println("2.有权限>>>>>>>就业信息");
            return true;
        }
        if(uri.contains("communication")&&power.contains("7")){
            System.out.println("2.有权限>>>>>>>国际交流");
            return true;
        }
        if(uri.contains("science")&&power.contains("8")){
            System.out.println("2.有权限>>>>>>>科研工作");
            return true;
        }
        if(uri.contains("cpc")&&power.contains("9")){
            System.out.println("2.有权限>>>>>>>党群工作");
            return true;
        }
        if(power.contains("all")){
            System.out.println("2.有权限>>>>>>>超级管理员全部通过");
            return true;
        }


        System.out.println("2.无权限>>>>>>>跳转");
        response.sendRedirect(request.getContextPath()+"/index");
        return true;


    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}
